package com.situ.web.service.impl;

import java.util.List;

import javax.naming.NameAlreadyBoundException;

import com.situ.web.dao.IStudentDao;
import com.situ.web.dao.impl.StudentDaoImpl;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.exception.NameAlreadyExistException;
import com.situ.web.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private IStudentDao studentDao = new StudentDaoImpl();

	@Override
	public boolean regist(Student student) throws NameAlreadyExistException {
		boolean result = false;
		boolean lastResult = false;
		if (studentDao.regist(student.getName())) {
			throw new NameAlreadyExistException("该用户名已存在");
		} else {
			result = studentDao.insert(student);
			if (result) {
				lastResult = true;
			}
		}
		return lastResult;
	}

	@Override
	public boolean delete(String id) {
		studentDao.delete(id);
		return true;
	}

	@Override
	public List<Student> findAll() {
		return studentDao.findAll();
	}

	@Override
	public Student findById(int id) {
		return studentDao.finById(id);
	}

	@Override
	public boolean updateById(Student student) {
		return studentDao.updateById(student) ? true : false;
	}

	@Override
	public PageBean findPageBean(int pageNo, int pageSize) {
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setPageNo(pageNo);
		int count = studentDao.totalCount();
		int totalPage = (int)Math.ceil((double)count / pageSize);
		pageBean.setTotalPage(totalPage);
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentDao.pageList(offset,pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

}
